#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
import os
import random
import time
from multiprocessing import Process, Queue


def product(q):
    for i in range(2):
        time.sleep(random.randint(1, 3))
        print("\033[45m{} 生产了 包子 {}\033[0m".format(os.getpid(), i))
        q.put(i)


def consume(q):
    while True:
        time.sleep(random.randint(1, 3))
        res = q.get()
        if res is None: break
        print("{} 吃掉了 包子 {}".format(os.getpid(), res))


if __name__ == '__main__':
    q = Queue()
    p1 = Process(target=product, args=(q,))
    p2 = Process(target=product, args=(q,))
    p3 = Process(target=product, args=(q,))
    c1 = Process(target=consume, args=(q,))
    c2 = Process(target=consume, args=(q,))

    p1.start()
    p2.start()
    p3.start()


    c1.start()
    c2.start()

    p1.join()
    p2.join()
    p3.join()

    # 有多少个消费者就得传多少个None
    q.put(None)
    q.put(None)
